package com.elephantgreen.common;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Store information on a cash payment.
 * 
 */
public class CashEntry extends ElephantgreenCommon {

	private Sale sale;
	private Client client;
	private double cashAmount;

	/**
	 * Create new cash entry with sale and client data.
	 * 
	 * @param client
	 * @param sale
	 * @param amount
	 */
	public CashEntry(Client client, Sale sale, double amount) {
		super();
		this.sale = sale;
		this.client = client;
		this.cashAmount = amount;
	}

	/**
	 * Create new cash entry with only client data.
	 * 
	 * @param client
	 * @param amount
	 */
	public CashEntry(Client client, double amount) {
		super();
		this.client = client;
		this.cashAmount = amount;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.elephantgreen.common.EgCommon#createXml()
	 */
	@Override
	public Document createXml() {
		// added sale.getInvoiceNumber() instead of saleId

		Document doc = null;

		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			doc = docBuilder.newDocument();

			// root element
			Element rootElement = doc.createElement("cashPayments");
			doc.appendChild(rootElement);

			// cash element
			Element cash = doc.createElement("cash");
			rootElement.appendChild(cash);

			// cashSales element
			Element cashSales = doc.createElement("cashSales");
			rootElement.appendChild(cashSales);

			// clientId element
			Element clientId = doc.createElement("clientId");
			clientId.appendChild(doc.createTextNode(getClient().getClientId()));
			cash.appendChild(clientId);

			// cashAmount element
			Element cashAmount = doc.createElement("cashAmount");
			cashAmount.appendChild(doc.createTextNode(getCashAmount()+""));
			cash.appendChild(cashAmount);

			// saleId element
			Element invoiceNumber = doc.createElement("invoiceNumber");
			invoiceNumber.appendChild(doc.createTextNode(getSale().getInvoiceNumber()));
			cashSales.appendChild(invoiceNumber);

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		}
		return doc;
	}

	public Sale getSale() {
		return sale;
	}

	public void setSale(Sale sale) {
		this.sale = sale;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public double getCashAmount() {
		return cashAmount;
	}

	public void setCashAmount(double cashAmount) {
		this.cashAmount = cashAmount;
	}

}
